package com.freshmarket.freshmarket.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freshmarket.freshmarket.entity.Coupon;

import com.freshmarket.freshmarket.controller.form.FormCoupon;
import com.freshmarket.freshmarket.service.ICouponService;
import com.freshmarket.freshmarket.utils.Result;
import com.freshmarket.freshmarket.utils.ValidUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Squirrel
 * @since 2020-09-04
 */
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/freshmarket/coupon")
public class CouponController {
    @Autowired
    private ICouponService iCouponService;
    @GetMapping("/addCoupon")
    public Result insert( @Valid FormCoupon formCoupon, BindingResult result){

    if(result.hasErrors()){
    String extract = ValidUtil.extract(result);
    Result.failedResult(1111,extract);
    }

        log.info("传入的值：{}",formCoupon);
        Coupon coupon = new Coupon();
//        coupon.setCouponName("九折卷");
//        coupon.setCouponNum(999);

        BeanUtils.copyProperties(formCoupon,coupon);
//        System.out.println(coupon.toString());
    iCouponService.save(coupon);

    return Result.successResult();


    }
    @GetMapping("/select")
    public Result select(@RequestParam(defaultValue = "1") int pageIndex,@RequestParam(defaultValue = "3") int pageSize,String couponStatus,String couponDiscountsStatus ){


        IPage<Coupon> couponIPage = iCouponService.selectCoupon(pageIndex, pageSize, couponStatus,couponDiscountsStatus);

        return Result.successResult(couponIPage);

    }
    @GetMapping("/selectAll")
    public Result selectAll(@RequestParam(defaultValue = "1") int pageIndex,@RequestParam(defaultValue = "3") int pageSize){
        IPage<Coupon> couponIPage = iCouponService.selectCouponAll(pageIndex, pageSize);
        log.info("当前页码："+pageIndex+"当前条数"+pageSize);
        return Result.successResult(couponIPage);
    }



}

